using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Text;

using Peradigm.Framework.Persistence;

using Liberty.Domain.Kernel;

namespace Liberty.Persistence.Kernel
{
    public class TaskBroker : AbstractBroker
    {
        #region Constants
        #endregion

        #region Event Handlers
        #endregion

        #region Methods
        #endregion

        #region Properties
        #endregion

        #region Private Methods

        private Task TaskFrom( SafeReader reader )
        {
            long taskId;
            Task task;

            // taskId = this.OIDFrom( reader );
            task = new Task(); // TODO: Populate properties.

            return task;
        }

        private List<Task> TasksFrom( SqlCommand cmd )
        {
            SafeReader reader;
            List<Task> tasks;

            reader = null;
            try
            {
                reader = this.ExecuteReader( cmd );
                tasks = new List<Task>();
                while( reader.Read() )
                {
                    tasks.Add( this.TaskFrom( reader ) );
                }
            }
            finally
            {
                if( reader != null )
                {
                    this.Close( reader );
                    reader = null;
                }
            }

            return tasks;
        }

        #endregion

        #region Private Properties
        #endregion

        #region Construction and Finalization

        public TaskBroker( SqlTransaction txn )
            : base( txn )
        {
        }

        public TaskBroker( string cxnString )
            : base( cxnString )
        {
        }

        #endregion

        #region Public Methods

        /// 
        /// <param name="criteria"></param>
        public List<Task> TaskFor( TaskSearchCriteria criteria )
        {
            List<Task> tasks = new List<Task>();

            SqlCommand cmd = null;

            try
            {
                cmd = criteria.CommandFor();

                tasks = this.TasksFrom( cmd );
            }
            finally
            {
                this.Close( cmd );
            }

            return tasks;
        }

        #endregion
    }

}